#ifndef YHAIDA_EPOLL_H
#define YHAIDA_EPOLL_H
#include "../base/Utils.h"
#include "../datetime/Timestamp.h"
#include "../logging/Logging.h"
#include "EventLoop.h"
#include <vector>
#include <map>
#include <cassert>
#include <errno.h>
#include <poll.h>
#include <sys/epoll.h>
namespace yhaida
{
    //前向声明
    class Channel;

    class EPoller : Utils::nocapyable
    {
    public:
        //整个EventLoop维护着两个std::vector<Channel*>
        //  1.EPoller里面的IO复用后fd与channel匹配，激活后放入2.中去
        //  2.EventLoop里面activeChannels
        typedef std::vector<Channel *> ChannelList;
        typedef std::vector<struct epoll_event> EventList;
        typedef std::map<int, Channel *> ChannelMap;


    public:
        EPoller(EventLoop *loop);
        ~EPoller();

        //核心
        Timestamp poll(ChannelList *EventLoopActiveChannels, int timeout);

        void updataChannel(Channel *channel);
        void removeChannel(Channel *channel);

        void assertInLoopThread() { _mainEventLoop->assertInLoopThread(); }

    private:
        void fillActiveChannels(int numEvents, ChannelList *EventLoopActiveChannels) const;
        void update(int operation, Channel *channel);

        static const int kInitEventListSize = 16;

        EventLoop *_mainEventLoop;
        int _epollfd;//epoll_create();
        EventList _events;//与POLL不太一样，这个是存放epoll_wait()之后的结果
        ChannelMap _channels;
    };

}
#endif